package ui;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;

import control.Operator;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.Box;
import java.awt.Component;

public class AddBookUI extends JFrame {

	private JPanel contentPane;
	private JTextField booknameText;
	private JTextField authorText;
	private JTextField priceText;
	private JButton confirmButton;
	private JButton cancelButton;

	/**
	 * Create the frame.
	 */
	public AddBookUI() {
		setTitle("添加图书");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(450, 250, 450, 350);
		setLocationRelativeTo(null); // 居中显示

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(20, 20, 20, 20));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 15));

		// 标题面板
		JLabel titleLabel = new JLabel("添加新图书", SwingConstants.CENTER);
		titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		titleLabel.setForeground(new Color(70, 130, 180));
		contentPane.add(titleLabel, BorderLayout.NORTH);

		// 表单面板
		JPanel formPanel = new JPanel();
		formPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),
				"图书信息", TitledBorder.LEADING, TitledBorder.TOP,
				new Font("微软雅黑", Font.PLAIN, 14), new Color(100, 100, 100)));
		contentPane.add(formPanel, BorderLayout.CENTER);
		formPanel.setLayout(null);

		// 表单元素
		JLabel nameLabel = new JLabel("书名：");
		nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		nameLabel.setBounds(80, 40, 60, 25);
		formPanel.add(nameLabel);

		booknameText = new JTextField();
		booknameText.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		booknameText.setBounds(150, 40, 200, 30);
		formPanel.add(booknameText);
		booknameText.setColumns(10);

		JLabel authorLabel = new JLabel("作者：");
		authorLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		authorLabel.setBounds(80, 90, 60, 25);
		formPanel.add(authorLabel);

		authorText = new JTextField();
		authorText.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		authorText.setBounds(150, 90, 200, 30);
		formPanel.add(authorText);
		authorText.setColumns(10);

		JLabel priceLabel = new JLabel("价格：");
		priceLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		priceLabel.setBounds(80, 140, 60, 25);
		formPanel.add(priceLabel);

		priceText = new JTextField();
		priceText.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		priceText.setBounds(150, 140, 200, 30);
		formPanel.add(priceText);
		priceText.setColumns(10);

		// 按钮面板
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 0));
		contentPane.add(buttonPanel, BorderLayout.SOUTH);

		confirmButton = new JButton("确定");
		confirmButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		confirmButton.setBackground(new Color(70, 130, 180));
		confirmButton.setForeground(Color.WHITE);
		confirmButton.setFocusPainted(false);
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String bookname = booknameText.getText().trim();
				String author = authorText.getText().trim();
				float price = 0;

				// 验证输入
				if (bookname.isEmpty() || author.isEmpty()) {
					JOptionPane.showMessageDialog(null, "书名和作者不能为空！", "输入错误", JOptionPane.WARNING_MESSAGE);
					return;
				}

				try {
					price = Float.parseFloat(priceText.getText().trim());
					if (price <= 0) {
						JOptionPane.showMessageDialog(null, "价格必须大于0！", "输入错误", JOptionPane.WARNING_MESSAGE);
						return;
					}

					Operator operator = new Operator();
					boolean isSuccess = operator.addBook(bookname, author, price);
					if (isSuccess) {
						JOptionPane.showMessageDialog(null, "添加成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
						// 清空输入框
						booknameText.setText("");
						authorText.setText("");
						priceText.setText("");
					} else {
						JOptionPane.showMessageDialog(null, "添加失败！", "失败", JOptionPane.ERROR_MESSAGE);
					}
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "请输入有效的价格数字！", "输入错误", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		buttonPanel.add(confirmButton);

		cancelButton = new JButton("取消");
		cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		cancelButton.setBackground(new Color(220, 220, 220));
		cancelButton.setFocusPainted(false);
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddBookUI.this.dispose();
			}
		});
		buttonPanel.add(cancelButton);
	}
}
